package com.egreen.order.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author cloudJiang
 * @date 2022/9/20 23:21
 */
@Component
public class OrderCountRedisUtil {
    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;

    /**
     * 存储前缀
     */
    private static String KEY_CODE = "order:";

    /**
     * 判断当天是否有订单记录
     */
    public Boolean existDay(String operate, String date) {
        String key = KEY_CODE + date + ":" + operate + ":";
        return redisTemplate.hasKey(key);
    }

    /**
     * 判断当天是否有总订单记录
     */
    public Boolean existAll(String date) {
        String key = KEY_CODE + date + ":all:";
        return redisTemplate.hasKey(key);
    }

    /**
     * 判断是否有总的订单记录
     */
    public Boolean existAll() {
        String key = KEY_CODE + "all:";
        return redisTemplate.hasKey(key);
    }

    /**
     * 将当天的订单记录存储加一
     */
    public void addDay(String operate, String date) {
        if (this.existDay(operate, date)) {
            String key = KEY_CODE + date + ":" + operate + ":";
            Integer newCount = this.getOrderCountDay(operate, date) + 1;
            redisTemplate.opsForValue().set(key, newCount);
        } else {
            this.createDay(operate, date);
        }
    }

    /**
     * 将当天的订单记录存储减一
     */
    public void subDay(String operate, String date) {
        if (this.existDay(operate, date)) {
            String key = KEY_CODE + date + ":" + operate + ":";
            Integer newCount = this.getOrderCountDay(operate, date) - 1;
            redisTemplate.opsForValue().set(key, newCount);
        } else {
            return;
        }
    }

    /**
     * 将当天的总订单记录存储加一（带参，日期）
     */
    public void addAll(String date) {
        String key = KEY_CODE + date + ":all:";
        Integer newCount = this.getOrderCountAll(date) + 1;
        redisTemplate.opsForValue().set(key, newCount);
    }

    /**
     * 将总订单记录存储加一，不带日期
     */
    public void addAll() {
        String key = KEY_CODE + "all:";
        Integer newCount = this.getOrderCountAll() + 1;
        redisTemplate.opsForValue().set(key, newCount);
    }

    /**
     * 创建当天的总订单计数
     */
    public void createAll(String date) {
        String key = KEY_CODE + date + ":all:";
        redisTemplate.opsForValue().set(key, 1);
    }

    /**
     * 创建总订单计数
     */
    public void createAll() {
        String key = KEY_CODE + "all:";
        redisTemplate.opsForValue().set(key, 1);
    }

    /**
     * 创建当天订单计数
     */
    public void createDay(String operate, String date) {
        String key = KEY_CODE + date + ":" + operate + ":";
        redisTemplate.opsForValue().set(key, 1);
    }

    /**
     * 获取某一天的订单记录
     *
     * @param operate
     * @param date
     * @return 订单计数
     */
    public Integer getOrderCountDay(String operate, String date) {
        String key = KEY_CODE + date + ":" + operate + ":";
        //从缓存中拿到数据
        Integer result = redisTemplate.opsForValue().get(key);
        if (result == null) {
            return 0;
        }
        return result;
    }

    /**
     * 获取某天全部的订单记录
     *
     * @param date
     * @return 订单计数
     */
    public Integer getOrderCountAll(String date) {
        String key = KEY_CODE + date + ":all:";
        //从缓存中拿到数据
        Integer result = redisTemplate.opsForValue().get(key);
        if (result == null) {
            return 0;
        }
        return result;
    }

    /**
     * 获取全部的订单记录
     *
     * @return 订单计数
     */
    public Integer getOrderCountAll() {
        String key = KEY_CODE + "all:";
        //从缓存中拿到数据
        Integer result = redisTemplate.opsForValue().get(key);
        if (result == null) {
            return 0;
        }
        return result;
    }
}
